$(document).ready(function()
{
	$('#news-feed').each(function()
	{

		var $container = $(this);
		$container.empty();

		/**
		 * 内部渐变效果
		 * 效果区高度是 25%
		 *
		 */
		 console.log('asdf');
		var fadeHeight = $container.height() / 4;
		for (var yPos = 0; yPos < fadeHeight; yPos += 2)
		{
			
			$('<div></div>').css({
				opacity: yPos / fadeHeight,
				top: $container.height() - fadeHeight + yPos
			}).addClass('fade-slice').appendTo($container);
		}
		
		// 加载进度指示器
		/**
		 * http://ajaxload.info
		 *
		 *
		 */
		var $loadingIndicator = $('<img>').attr(
		{
			'src': 'ajax-loader.gif',
			'alt': 'Loading. Please wait.'
		}).addClass('news-wait').appendTo($container);
		
		$.get('./cnblogs-rss.xml', function(data)
		{
			$loadingIndicator.remove();
			$('feed entry', data).each(function()
			{
				var $link = $('<a></a>').attr('href', $('id', this).text()).text($('title', this).text());
				var $headline = $('<h4></h4>').append($link);

				var pubDate = new Date($('published', this).text());
				var pubMonth = pubDate.getMonth() + 1;
				var pubDay = pubDate.getDate();
				var pubYear = pubDate.getFullYear();
				var $publication = $('<div></div>').addClass('publication-date').text(pubMonth +
				'/' + pubDay + '/' + pubYear);
				var $summary = $('<div></div>').addClass('summary').html($('content', this).text().substr(0, 80));
				$('<div></div>').addClass('headline').append($headline, $publication, $summary).appendTo($container);

				// console.log($('title', this).text());
			});

			var current = 0;
			var old = 0;
			var hiddenPosition = $container.height() + 10;
			$('div.headline').eq(current).css('top', 0);
			var headlineCount = $('div.headline').length;
			var pause;
			var rotateInProgress = false; // 悬停时暂停
			var headlineRotate = function()
			{
				if (!rotateInProgress)
				{
					pause = false;
					rotateInProgress = true;
					current = (old + 1) % headlineCount;
					$('div.headline').eq(old).animate({top: -hiddenPosition}, 'slow', function()
					{
						$(this).css('top', hiddenPosition);
					});
					$('div.headline').eq(current).animate({top: 0}, 'slow', function()
					{
						rotateInProgress = false;
						if (!pause)
						{
							pause = setTimeout(headlineRotate, 1000*3);
						}
						
					});
					old = current;
				}
			};
			if (!pause)
			{
				pause = setTimeout(headlineRotate, 1000*3);
			}
			
			$container.hover(function()
			{
				clearTimeout(pause);
				pause = false;
			}, function()
			{
				if (!pause)
				{
					pause = setTimeout(headlineRotate, 250);
				}
				
			});
		});
	});
	
});